package com.ophiux.cs.application.chat.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.chat.dto.SendCSMessageRequest;
import com.ophiux.cs.application.chat.dto.SendCSMessageResponse;
import com.ophiux.cs.application.chat.service.ISendCSMessageAppService;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.domain.chat.domainservice.SendCSMessageDomainService;
import com.ophiux.cs.domain.chat.model.entity.CSChatMessageInfo;
import com.ophiux.cs.domain.chat.model.entity.CSChatMessageSend;
import com.ophiux.cs.domain.chat.model.entity.CSChatSession;
import com.ophiux.cs.domain.chat.model.valueobj.CSUserValue;
import com.ophiux.cs.domain.chat.model.valueobj.PushCSChatValueObj;
import com.ophiux.cs.domain.chat.model.valueobj.PushChatMessageResult;
import com.ophiux.cs.domain.chat.remote.IPushChatMessageRemoteService;
import com.ophiux.cs.domain.chat.remote.IUserRemoteService;
import com.ophiux.cs.domain.chat.repositories.ICSChatSessionRepository;
import com.ophiux.utils.exception.OphiuxParamException;
import com.ophiux.utils.exception.OphiuxRemoteException;
import com.ophiux.utils.exception.OphiuxRepositoryException;

/**
 * 发送图文聊天消息
 * @author chenh
 *
 */
@Service("sendCSMessageAppService")
public class SendCSMessageAppService implements ISendCSMessageAppService{

	@Autowired
	private ICSChatSessionRepository cSChatSessionRepository;
	
	@Autowired
	private SendCSMessageDomainService sendCSMessageDomainService;
	
	@Autowired
	private IUserRemoteService userRemoteService;
	
	@Autowired
	private IPushChatMessageRemoteService pushChatMessageRemoteService;	//消息发送
	
	@Override
	public Result<SendCSMessageResponse> sendConMessage(SendCSMessageRequest request) {
		//初始化
		Result<SendCSMessageResponse> result = null;
		
		/**参数合法性验证**/
		if(!request.isParamValid()) {
			return Result.fail("", "参数不合法");
		}
		String userID = request.getTO_USER();
		CSUserValue toUser = userRemoteService.getCSUser(userID);
		
		//获取发送人会话
		CSChatSession fromSession = null;
		//接收人会话
		CSChatSession toSession = null;
		
		try {
			
			fromSession = cSChatSessionRepository
					.getCSChatSession(request.getFROM_USER(), request.getTO_USER());
			if(fromSession == null) {
				//发送人会话不存在，创建会话
				fromSession = new CSChatSession(request.getFROM_USER(), request.getTO_USER());
			}
			
			toSession = cSChatSessionRepository
					.getCSChatSession(request.getTO_USER() , request.getFROM_USER());
			
			if(toSession == null) {
				//发送人会话不存在，创建会话
				toSession = new CSChatSession( request.getTO_USER() , request.getFROM_USER());
			}
			
		} catch (OphiuxRepositoryException e1) {
			e1.printStackTrace();
			return Result.fail("", e1.getExceptionMessage());
		}
		
		String dIALOG_ID = request.getDIALOG_ID();
		
		CSChatMessageInfo messageInfo = 
				new CSChatMessageInfo(dIALOG_ID, request.getFROM_USER(),
		
						request.getMSG_TYPE(), request.getCONTENT(),request.getSEND_SCOPE());
	
		//发送消息
		try {
			
			sendCSMessageDomainService.sendCSMessage(fromSession, toSession, messageInfo);
		
		} catch (OphiuxParamException e) {
			e.printStackTrace();
			return Result.fail("", e.getExceptionMessage());
		}
		
		try {
			cSChatSessionRepository.save(fromSession);
			cSChatSessionRepository.save(toSession);
		} catch (OphiuxRepositoryException e) {
			e.printStackTrace();
			return Result.fail("", e.getExceptionMessage());
		}
		
		//推送即时消息
		try {
			PushCSChatValueObj push = new PushCSChatValueObj();
			push.setFromUser(request.getFROM_USER());
			push.setToUser(request.getTO_USER());
			push.setContent(request.getCONTENT());
			
			PushChatMessageResult pushRes = pushChatMessageRemoteService.push(push);
		} catch (OphiuxRemoteException e) {
			e.printStackTrace();
			//推送即时消息异常无需处理
		}
		
		return Result.success();
	}

	
}
